//
// Created by subject on 2025/5/16.
//

#ifndef MY_APPLICATION_JAVAVMHELPER_H
#define MY_APPLICATION_JAVAVMHELPER_H

#include <jni.h>
#include <atomic>
#include <mutex>
#include <unordered_set>
#include <thread>
//线程安全的全局 JNIEnv 管理

class JavaVMHelper {
public:
    static JavaVMHelper &getInstance();

    void setJavaVm(JavaVM *javaVM); // 设置 JavaVM 指针
    JNIEnv *getJniEnv(); // 获取 JNIEnv 指针
    void detachCurrentThread(); //清理方法，释放变量

private:
    std::atomic<JavaVM *> vm_;
    std::mutex mutex_;
    std::unordered_set<std::thread::id> attachedThreads_;
};

#endif //MY_APPLICATION_JAVAVMHELPER_H
